package cn.ava.ymcc.service.impl;

import cn.ava.ymcc.service.ITokenService;
import cn.ava.ymcc.utils.StrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class TokenServiceImpl implements ITokenService {

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    public static final String TOKEN_KEY = "token:%s:%s";

    /**
     * 防重订单token生成
     * @param courseId
     * @return
     */
    @Override
    public String createToken(String courseId) {
        Long loginId = 2353L;
        String key = String.format(TOKEN_KEY, loginId, courseId);
        // 生成8位随机字符串token
        String token = StrUtils.getComplexRandomString(8);
        // 存入redis,token过期时间为15分钟
        redisTemplate.opsForValue().set(key,token,15, TimeUnit.MINUTES);
        return token;
    }
}
